<!--
 * @Author: tansq
 * @Date: 2021-09-17 13:54:28
 * @LastEditTime: 2022-09-09 16:18:32
 * @LastEditors: tansq
 * @Description: 公司批量入库
 * @FilePath: \hte-vue-dev\src\views\library\company\information\BatchCompany.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->

<template>
  <div class="page-main">
    <home-nav :userData="userinfo"></home-nav>
    <div class="container">
      <div class="head-content">
        <span class="bold-font">公司批量入库(匹配规则为基本信息中的公司名称)</span>
        <!-- <div class="btn-line">
          <a-button class="hte-btn add-btn" type="primary">提交</a-button>
        </div> -->
      </div>
      <div class="content">

         <BatchBaseInfo ref="baseInfo" :dics="dics"></BatchBaseInfo>
         
        <a-form-model
          class="hte-form-model"
          ref="ruleForm"
          
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >

         <section>
            <div class="section-header">
              企业证照附件
            </div>
            <AttachBatchUpload ref="enterpriseLicenseAttach"  :accept="'.jpg,.jpeg,.png'"/>
          </section>
        </a-form-model>   

        <CompanySizeInfo ref="companySizeInfo" ></CompanySizeInfo>
         <CompanyShareholder ref="companyShareholder"></CompanyShareholder>
         <a-form-model
          class="hte-form-model"
          ref="ruleForm"
          
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >

         <section>
            <div class="section-header">
              股权穿透图附件
            </div>
            <AttachBatchUpload ref="companyShareholderAttach" :accept="'.jpg,.jpeg,.png'" />
          </section>
        </a-form-model>   
         <CompanyTeam :dics="dics"  ref="companyTeam"></CompanyTeam>
          <CompanyHoldingParticipation  ref="companyHoldingParticipation"></CompanyHoldingParticipation>     
      </div>      
    </div>  
    <div class="btn-box">
      <a-button class="hte-btn add-btn" @click="cancelBatch">取消入库</a-button>
      <a-button v-has="'company:addBatch'" class="hte-btn add-btn" style="margin-left:8px" type="primary" @click="addBatch" :loading="tabLoading">入库</a-button>
    </div>  
     <a-back-top  style="right:50px;"/>
  </div>
</template>
<script>
import '@/config/upload.install.js'
import AttachBatchUpload from '@/components/tools/AttachBatchUpload';
import { deleteAction, getAction,postAction,downFile,getFileAccessHttpUrl } from '@/api/manage'
import { companyMixin ,companyValidate} from '@/views/library/company/companyMixin'
export default {
  name:'BatchCompany',
  components: {
    HomeNav: () => import('@/components/layouts/HomeNav'),
    AttachBatchUpload,
    BatchBaseInfo:()=>import('@/views/library/company/components/BatchBaseInfo'),
    CompanySizeInfo:()=>import('@/views/library/company/components/CompanySizeInfo'),
    CompanyShareholder:()=>import('@/views/library/company/components/CompanyShareholder'),
    CompanyTeam:()=>import('@/views/library/company/components/CompanyTeam'),
    CompanyHoldingParticipation:()=>import('@/views/library/company/components/CompanyHoldingParticipation'),
  },
  mixins: [companyMixin],
  data() {
    return {
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'CompanyEditor',
      },
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },

      tabLoading: false,
      currPageIndex: 1,
      currPageSize: 10,
      url:{
        'addBatch':'/libraryCompany/hteLibCompany/addBatch'
      },
      visibleModel: false,
      visibleForm: {
        name: '',
        position: '',
      },
      companyList:[]
    }
  },
  created() {
    const obj = this.$route.query
    this.fileId = obj.id
  },
  mounted() {
  
  },
  methods: {
    addBatch(){
      let that = this;
     if(!this.handleData()){
        return;
      }
      this.$confirm({
        title: '是否确认入库？',
        onOk() {
          that.tabLoading = true;
          postAction(that.url.addBatch,that.companyList).then(res=>{
            if(res.success){
             
              that.$message.success('入库成功！');
               that.$router.push('/company/list')
            }else{
              that.$message.warning(res.message);
            }
          }).finally(res=>{
              that.tabLoading = false;
          })
        },
      })
    },
    cancelBatch(){
      let that = this;
      
      this.$confirm({
        title: '是否取消入库？',
        onOk() {
          that.$refs.enterpriseLicenseAttach.deleteAall();
          that.$refs.companyShareholderAttach.deleteAall();
          that.$router.push('/company/list')

        },
      })
    },
    handleData(){
      const that = this
      const maxData = 100
      
      that.companyList = []
      let baseInfoData = that.$refs.baseInfo.sourceData
      let companySizeInfoData = that.$refs.companySizeInfo.sourceData
      let companyShareholderData = that.$refs.companyShareholder.sourceData
      let companyTeamData = that.$refs.companyTeam.sourceData
      let companyHoldingParticipation = that.$refs.companyHoldingParticipation.sourceData 
      let enterpriseLicenseAttach = that.$refs.enterpriseLicenseAttach.fileList
      let companyShareholderAttach = that.$refs.companyShareholderAttach.fileList
      if(baseInfoData.length == 0 || baseInfoData.length > maxData){
        this.$message.warning(`请选择数据、每次批量导入公司数量不大于${maxData}！`);
       throw(`请选择数据、每次批量导入公司数量不大于${maxData}！`)
      }
      for (let index = 0; index < baseInfoData.length; index++) {
        const element = baseInfoData[index];
        //新增状态
        element.companyStatus  = 3
        element.addBatchFlag = true;
        const companyName = element.name
        let sizeInfo = companySizeInfoData.find(item=>{return item=> item.companyName == companyName})
        element.companySizeInfo = sizeInfo || {}
        let companyShareholder = companyShareholderData.filter(item=>{
          return  item.companyName == companyName
        })
        
     
        for (let index = 0; index < companyShareholder.length; index++) {
          const element = companyShareholder[index];
          let arr = companyShareholder.filter(item=>{
            return  item.name == element.name && item.companyName == companyName
          })
          if(!!arr && arr.length > 1){
            this.$message.warning(`${companyName}公司股东名称重复${ element.name}！`);
            return false
          }
        }
        element.companyShareholders = companyShareholder

        let companyTeams = companyTeamData.filter(item=>{
           return item.companyName == companyName
        })
        for (let index = 0; index < companyTeams.length; index++) {
          const element = companyTeams[index];
          let arr = companyTeamData.filter(item=>{
            return  item.typeName == element.typeName && item.companyName == companyName
          })
          if(!!arr && arr.length > 1){
            this.$message.warning(`${companyName}公司职务重复${ element.typeName}！`);
            return false
          }
        }
      
        element.companyTeams = companyTeams

        let prticipations = companyHoldingParticipation.filter(item=>{
           return item.companyNameBase == companyName
        })
 
        for (let index = 0; index < prticipations.length; index++) {
          const element = prticipations[index];
          let arr = companyHoldingParticipation.filter(item=>{
            return  item.companyName == element.companyName && item.companyNameBase == companyName
          })
          if(!!arr && arr.length > 1){
            this.$message.warning(`${companyName}公司;控股参股公司名称重复${ element.companyName}！`);
            return flase
          }
        }
        element.prticipations = prticipations

        let enterpriseLicenseFile =  enterpriseLicenseAttach.find(item =>{
          let fileName = item.fileName.substring(0,item.fileName.lastIndexOf('.')).replace(/\s+/,'')
           return fileName == companyName
        })
        //执照
        if(!!enterpriseLicenseFile){
          element.enterpriseLicense = enterpriseLicenseFile.id
        }
         let companyShareholderFile =  companyShareholderAttach.find(item =>{
          let fileName = item.fileName.substring(0,item.fileName.lastIndexOf('.')).replace(/\s+/,'')
            return fileName == companyName
        })
        //股权
        if(!!companyShareholderFile){
          element.stockRightImage = companyShareholderFile.id
        }
      }

      that.companyList = baseInfoData;

      return true
    },
    handleDelete(obj, list) {
      const that = this
      this.$confirm({
        title: '是否确认删除？',
        onOk() {
          that[list].splice(
            that[list].findIndex((item) => item.id === obj.id),
            1
          )
        },
      })
    },
    handleClassOk() {
      this.visibleModel = false
    },
    handlePersonnel(type, item) {
      if (type === 'edit') {
        Object.assign(this.visibleForm, item)
      }
      this.visibleModel = true
    },
    handleSelect(){

    },
    /**
     * 附件绑busKey 
     */
    bindBusKey() {
      if (this.fileList.length > 0) {
        const ids = this.fileList.map(function (item) {
          return item.id
        })
        const jsona = { dbKey: 'Trd', id: ids.toString(), busKey: this.form.id }
        var jsonStr = JSON.stringify(jsona)
        this.$refs.myUpload.bindBusKey(jsonStr)
      }
    },
  },
  watch: {
    fileList: function (newQuestion, oldQuestion) {
      newQuestion.forEach((item) => {
        var newIndex = this.fileList.findIndex((item2) => item2.id === item.id)
        if (newIndex == -1) {
          this.fileList.push(item)
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
@import '~@views/library/company/company.less';

</style>
